package org.jeecg.modules.supplier.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.supplier.entity.PdProduct;
import org.jeecg.modules.supplier.service.PdProductService;
import org.jeecg.modules.supplier.vo.ProductVo;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 医院产品表 前端控制器
 * </p>
 *
 * @author twm
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/pd-product")
public class PdProductController extends BaseController{


    @Autowired
    public PdProductService productService;

    @GetMapping("find_produce_info")
    public Result findProdeceInfiBySupplierId(@RequestParam("currentPage") Integer currentPage,
                                              @RequestParam("paseSize") Integer paseSize,
                                              @RequestParam(value = "productName",required = false) String productName,
                                              @RequestParam(value = "specification",required = false) String specification,
                                              @RequestParam(value = "version",required = false) String version) {
        //
        String supplierId = "44eb164f206b406eb13eeb0c0d381502";
        SysUser currentUser = getCurrentUser();
        if(Objects.isNull(currentUser)) {
            return Result.error("错误");
        }
        supplierId = currentUser.getSocialCreditCode();

        IPage<PdProduct> pageCondition = new Page<>(currentPage, paseSize);

        IPage<PdProduct> page = productService
                .lambdaQuery()
                .eq(PdProduct::getSocialCreditCode,supplierId)
                .like(PdProduct::getName,productName)
                .like(PdProduct::getSpec,specification)
                .like(PdProduct::getVersion,version)
                .page(pageCondition);
        long total = page.getTotal();
        List<PdProduct> listProdect = page.getRecords();
        if(CollectionUtils.isNotEmpty(listProdect)) {
            Map<String,Object> returnValue = new HashMap<>(2);
            List<ProductVo> returnList = listProdect
                    .parallelStream()
                    .map(item -> {
                        return ProductVo.builder()
                                .productName(item.getName())
                                .productNumber(item.getNumber())
                                .specification(item.getSpec())
                                .version(item.getVersion())
                                //todo 单位和生产厂家都是id后面要修改*/
                                .unit(item.getUnitId())
                                .vender(item.getVenderId()).build();
                    }).collect(Collectors.toList());
            returnValue.put("total",total);
            returnValue.put("value",returnList);
            return Result.ok(returnValue);
        }

        return Result.ok("没有数据");
    }
}

